jQuery(document).ready(function(){
	jQuery("img.lazy").lazyload();
	jQuery("img.sig-image").lazyload();
	
	jQuery('.carousel,#product-carousel-generic').carousel({
		interval:2000,
		toughness: 0.25
	});
});

jQuery(window).load(function(){
	
	jQuery('.need_login,#signUp_alt').click(function(){
		jQuery('#loginModal').modal('toggle');
	});
	
	jQuery(location).attr('href');
	var hashkey = window.location.hash;

	if(hashkey == '#registration'){
		jQuery('#signupModal').modal('toggle');
	}else if(hashkey == '#login'){
		jQuery('#loginModal').modal('toggle');
	}
	
	jQuery('#quality-num-add').click(function(){
		var currentq = jQuery('#quality-num').val();
		currentq = parseInt(currentq);
		currentq++;
		jQuery('#quality-num').val(currentq);
		
	});
	
	jQuery('#quality-num-add-').click(function(){
		var currentq = jQuery('#quality-num').val();
		currentq = parseInt(currentq);
		currentq--;
		if(currentq <= 0)
			currentq = 1;
		jQuery('#quality-num').val(currentq);
		
	});
	
	jQuery('#extra-quality-num-add-2').click(function(){
		var currentq = jQuery('#extra-quality-num').val();
		currentq = parseInt(currentq);
		currentq++;
		
		jQuery('#extra-quality-num').val(currentq);
	});
	
	jQuery('#extra-quality-num-add-2-').click(function(){
		var currentq = jQuery('#extra-quality-num').val();
		currentq = parseInt(currentq);
		currentq--;
		if(currentq <= 0)
			currentq = 1;
		
		jQuery('#extra-quality-num').val(currentq);
	});
	
   
	jQuery('#point_from_date,#point_to_date').datepicker({
        format: 'dd-mm-yyyy',
        autoclose:true
      })
      .on('changeDate', function (ev) {
		    jQuery(this).datepicker('hide');
		    jQuery("#special_range").val("0");
		});;
      

	jQuery('#loginfrm').submit(function(e){
  		//check email
	  var email = jQuery('#username').val();
	  var password = jQuery('#password_login').val();
	  var subpage = jQuery('#slug_bad_sub').val();
	  
	  var btn = jQuery(this);
	  btn.button('loading');
      e.preventDefault();		
	  jQuery.ajax({
		url: "index.php?option=com_wrd&task=profile.checkInputUserName",
		type: "POST",
		data: 'email='+email+'&ajax=1',
		success: function (res) {
			
			var obj = jQuery.parseJSON(res);
			if(obj.status == 'error'){
				jQuery('#error_message').html('<p>'+obj.msg+'</p>');
				jQuery('#errorModal').modal('show');
				btn.button('reset');
			}else{
				//check pass
		          
		          jQuery.ajax({
		  			url: "index.php?option=com_wrd&task=profile.checkpass",
		  			type: "POST",
		  			data: 'password='+password+'&ajax=1&username='+obj.username,
		  			success: function (res) {
		  				var obj = jQuery.parseJSON(res);
		  				if(obj.status == 'error'){
		  					jQuery('#error_message').html('<p>'+obj.msg+'</p>');
		  					jQuery('#errorModal').modal('show');
		  					btn.button('reset');
		  				}
		  				else{
		  					//proccess login
		  					
		  					jQuery.ajax({
		  						url: "index.php?option=com_wrd&task=profile.login",
		  						type: "POST",
		  						data: 'password='+password+'&username='+email+'&ajax=1',
		  						success: function (res) {
		  							var obj = jQuery.parseJSON(res);
		  							if(obj.status == 'error'){
		  								jQuery('#error_message').html('<p">'+obj.msg+'</p>');
		  								jQuery('#errorModal').modal('show');
		  								btn.button('reset');
		  							}
		  							else{
		  								if(subpage == 1){
		  									location.reload(true);
		  								}else{
		  									if(obj.redirect == 1)
		  										window.location.replace(base_url+"index.php?option=com_wrd&view=profile&layout=default&id=1&Itemid=170");
		  									else
		  										location.reload(true);
							     	 	}
		  							}
		  						}
		  		  		   });
		  				}
		  			}
		  		   });
			}
		}
	   });
  });
  

  
  jQuery('#regfrm').submit(function(e){
  		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
		var email1 = jQuery('#email1').val();
		var password = jQuery('#password').val();
		var password1 = jQuery('#password1').val();
		
		e.preventDefault();
		//check email
		if(!emailReg.test(email1) ) {
		  	jQuery('#error_message').html('<p>'+JS_ALERT_EMAIL_IS_INVALID+'</p>');
		  	jQuery('#errorModal').modal('show');
		} else {
			var isChecked = jQuery('#term').is(':checked');
			//check password
			if(password == '' || password1 == ''){
				
		  		jQuery('#error_message').html('<p>'+JS_ALERT_PASSWORD_EMPTY+'</p>');
				jQuery('#errorModal').modal('show');
			}
			else if(password != password1){
			
		  		jQuery('#error_message').html('<p>'+JS_ALERT_THE_SAME_PASSWORD+'</p>');
				jQuery('#errorModal').modal('show');
			}
			else if(!isChecked) {
		  		jQuery('#error_message').html('<p>'+JS_ALERT_PLEASE_TICK_CHECK_BOX+'</p>');
		  		jQuery('#errorModal').modal('show');
			}
			else{
				//check email is available
		  		var btn = jQuery(this);
				btn.button('loading');
				jQuery.ajax({
					url: "index.php?option=com_wrd&task=profile.checkInputUserName",
					type: "POST",
					data: 'email='+email1+'&ajax=1',
					success: function (res) {
						var obj = jQuery.parseJSON(res);
						if(obj.status == 'error'){
							jQuery('#regfrm').submit();
						}
						else{
		  					jQuery('#error_message').html('<p>'+JS_ALERT_EMAIL_IS_NOT_AVAILABLE+'</p>');
		  					jQuery('#errorModal').modal('show');
		  					btn.button('reset');
						}
					}
	  		   });
			}
		}
     });  
});